/**
 * Project Name:dubbox-demo
 * File Name:UserRestServiceImpl
 * Package Name:com.fft.dubbox.provider.service
 * Date:2016/6/2115:37
 * Copyright (c) 2016,  www.shfft.com All Rights Reserved.
 */
package com.fft.dubbox.provider.service;

import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.fft.dubbox.dto.User;
import com.fft.dubbox.servicce.UserRestService;
import com.fft.dubbox.servicce.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * ClassName: UserRestServiceImpl
 * Reason: Rest服务接口实现.
 * date: 2016/6/21 15:37
 *
 * @author libin
 * @since JDK 1.6
 */
@Path("users")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class UserRestServiceImpl implements UserRestService {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @GET
    @Path("{id : \\d+}")
    public User getUser(@PathParam("id") Long id) {
        if (RpcContext.getContext().getRequest(HttpServletRequest.class) != null) {
            System.out.println("Client IP address from RpcContext: " + RpcContext.getContext().getRequest(HttpServletRequest.class).getRemoteAddr());
        }
        if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) {
            System.out.println("Response object from RpcContext: " + RpcContext.getContext().getResponse(HttpServletResponse.class));
        }
        return userService.getUser(id);
    }
}
